package com.nf.health.app.sport_recipe.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.github.jdsjlzx.util.RecyclerViewStateUtils;
import com.github.jdsjlzx.view.LoadingFooter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.customview.DividerListItemDecoration;
import com.nf.health.app.http.Act;
import com.nf.health.app.sport_recipe.adapter.SportHistoryAdapter;
import com.nf.health.app.sport_recipe.bean.SportHistoryBean;
import com.nf.health.app.utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by changpeng on 2017/04/19
 * 运动处方历史
 */

public class SportHistoryActivity extends BaseActivity {
    private int mPageSize = 10;
    private int mPageNo = 1;
    private int mTotalCount;
    private int mCurrentCount;
    @Bind(R.id.titlebar_title_tv)
    TextView titlebarTitleTv;
    @Bind(R.id.recycler)
    LRecyclerView mRecyclerView;
    @Bind(R.id.v_time_line)
    View mLine;
    private LRecyclerViewAdapter mRecyclerViewAdapter;
    private List<SportHistoryBean> mList = new ArrayList<>();
    private boolean isLodaingMore = false;
    private boolean isRefresh = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.layout_common_header);
        setContent(R.layout.activity_sport_history);
        ButterKnife.bind(this);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        titlebarTitleTv.setText("历史运动处方");
        SportHistoryAdapter mAdapter =  new SportHistoryAdapter(this,mList);
        mRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.addItemDecoration(new DividerListItemDecoration(this,LinearLayoutManager.VERTICAL));
        mRecyclerView.setAdapter(mRecyclerViewAdapter);
    }

    private void initListener() {
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
                if(state == LoadingFooter.State.Loading) {
                    return;
                }
                mPageNo++;
                if(mCurrentCount < mTotalCount) {
                    // loading more
                    isLodaingMore = true;
                    RecyclerViewStateUtils.setFooterViewState(SportHistoryActivity.this, mRecyclerView, 10, LoadingFooter.State.Loading, null);
                    initData();

                } else {
                    //the end
                    RecyclerViewStateUtils.setFooterViewState(SportHistoryActivity.this, mRecyclerView, 10, LoadingFooter.State.TheEnd, null);

                }
            }
        });

        mRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(SportHistoryActivity.this, SportRecipeHisActivity.class);
                intent.putExtra(Constants.HPUID,mList.get(position).getHpuid());
//                intent.putExtra(Constants.FROM,from);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    private void initData() {
        provider.getInterveneHistoryList(mPageNo+"",mPageSize + "");
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        mTotalCount = Integer.parseInt(JsonUtils.getFiledData(object+"", "totalCount"));
        RecyclerViewStateUtils.setFooterViewState(mRecyclerView, LoadingFooter.State.Normal);
        if(Act.Flag.FLAG_GET_INTERVENE_HISTORYLIST.equals(actionName)) {
            String data = JsonUtils.getFiledData(object+"", "data");
            String list = JsonUtils.getFiledData(data, "list");
            Gson gson = new MyGsonBuilder().createGson();
            List<SportHistoryBean> mNewList = gson.fromJson(list, new TypeToken<List<SportHistoryBean>>() {
            }.getType());
            if(isLodaingMore){
                mList.addAll(mNewList);
                isLodaingMore = false;
            }else{
                mList.clear();
                mList.addAll(mNewList);
            }
            mCurrentCount = mList.size();
            mRecyclerViewAdapter.notifyDataSetChanged();
            if(mList.size() != 0){
                mLine.setVisibility(View.VISIBLE);
            }
        }

    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
    }
}
